package com.tools.developer.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class CommonUtils {

	/**
	 * 加密
	 * 
	 * @param key
	 * @param plain
	 * @return
	 */
	public static String encrypt(String key, String plain) {
		String cipher = null;
		try {
			cipher = Base64Util.encode(DesUtil.encrypt(plain, key.getBytes()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cipher;
	}

	/**
	 * 解密
	 * 
	 * @param key
	 * @param cipher
	 * @return
	 */
	public static String decrypt(String key, String cipher) {
		String backplain = null;
		try {
			backplain = DesUtil.decrypt(Base64Util.decode(cipher),
					key.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return backplain;
	}

	/**
	 * 同Md5Helper得到的是一样的
	 * 
	 * @param input
	 * @return
	 */
	public static String md5(byte[] input) {
		byte[] result = null;
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
			result = md5.digest(input);
		} catch (NoSuchAlgorithmException ex) {
			result = new byte[16];
		}
		return byte2String(result);
	}

	public static String sha1(byte[] input) {
		byte[] result = null;
		MessageDigest sha1;
		try {
			sha1 = MessageDigest.getInstance("SHA-1");
			result = sha1.digest(input);
		} catch (NoSuchAlgorithmException ex) {
			result = new byte[20];
		}
		return byte2String(result);
	}

	public static String byte2String(byte[] bytes) {
		StringBuilder sb = new StringBuilder();
		for (byte b : bytes) {
			sb.append(String.format("%1$02x", 0xff & b));
		}
		return sb.toString();
	}

	public static List<PackageInfo> getRunningThirdApps(Context context) {
		ActivityManager activityMgr = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningAppProcessInfo> processes = activityMgr
				.getRunningAppProcesses();
		List<PackageInfo> apps = new ArrayList<PackageInfo>();
		if (processes == null)
			return apps;

		PackageManager packageMgr = context.getPackageManager();
		PackageInfo packageInfo;
		ApplicationInfo appInfo;
		for (ActivityManager.RunningAppProcessInfo info : processes) {
			try {
				packageInfo = packageMgr.getPackageInfo(info.processName,
						PackageManager.GET_ACTIVITIES);
				appInfo = packageInfo.applicationInfo;
				if (appInfo != null) {
					if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1) {
						apps.add(packageInfo);
					}
				}
			} catch (Exception e) {
			}
		}
		return apps;
	}

	public static List<PackageInfo> getRunningAllApps(Context context) {
		ActivityManager activityMgr = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningAppProcessInfo> processes = activityMgr
				.getRunningAppProcesses();
		List<PackageInfo> apps = new ArrayList<PackageInfo>();
		if (processes == null)
			return apps;

		PackageManager packageMgr = context.getPackageManager();
		PackageInfo packageInfo;
		ApplicationInfo appInfo;
		for (ActivityManager.RunningAppProcessInfo info : processes) {
			try {
				packageInfo = packageMgr.getPackageInfo(info.processName,
						PackageManager.GET_ACTIVITIES);
				appInfo = packageInfo.applicationInfo;
				if (appInfo != null) {
					apps.add(packageInfo);
				}
			} catch (Exception e) {
			}
		}
		return apps;
	}

	public static int[] getPids(int uid, Context context) {

		ActivityManager activityMgr = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> sAppInfos = activityMgr
				.getRunningAppProcesses();// TODO 后期将此项显示到UI
		List<RunningServiceInfo> sServInfos = activityMgr
				.getRunningServices(Integer.MAX_VALUE);// TODO 后期将此项显示到UI

		ArrayList<Integer> pids = new ArrayList<Integer>();
		if (sAppInfos != null) {
			for (RunningAppProcessInfo pinfo : sAppInfos) {
				if (pinfo.uid == uid) {
					if (!pids.contains(pinfo.pid)) {
						pids.add(pinfo.pid);
					}
					break;
				}
			}
		}

		if (sServInfos != null) {
			for (RunningServiceInfo sinfo : sServInfos) {
				if (sinfo.uid == uid) {
					if (!pids.contains(sinfo.pid)) {
						pids.add(sinfo.pid);
					}
					break;
				}
			}
		}

		int size = pids.size();
		int[] pidArray = new int[size];
		for (int i = 0; i < size; i++) {
			pidArray[i] = pids.get(i);
		}

		return pidArray;
	}
	
	public static List<RunningServiceInfo> getRunningServiceInfo(Context context) {
		ActivityManager activityMgr = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		return activityMgr.getRunningServices(Integer.MAX_VALUE);
	}
}
